

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[TOC] 一、参考资料  视频资料  二、运行环境  Windows11 Visual Studio Code v2022 Node.js v16.5.01 Vue&#x2F;cli v5.0.6  需求案例说明： 使用 Vue2模块化编程方式来实现下图的案例（样式是参照教程随便写的，这里以熟悉Vue2为主）  项目结构：  说明： Vue2支持模块化编程，一个 Vue后缀的文件表示一个Vue">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue2 笔记1 基于Vue2的TodoList待办事项增删查案例 _ 基于Node.js 使用Vue-CLI脚手架构建项目">
<meta property="og:url" content="http://example.com/2022/06/21/Vue2%20%E7%AC%94%E8%AE%B01%20%E5%9F%BA%E4%BA%8EVue2%E7%9A%84TodoList%E5%BE%85%E5%8A%9E%E4%BA%8B%E9%A1%B9%E5%A2%9E%E5%88%A0%E6%9F%A5%E6%A1%88%E4%BE%8B%20_%20%E5%9F%BA%E4%BA%8ENode.js%20%E4%BD%BF%E7%94%A8Vue-CLI%E8%84%9A%E6%89%8B%E6%9E%B6%E6%9E%84%E5%BB%BA%E9%A1%B9%E7%9B%AE/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[TOC] 一、参考资料  视频资料  二、运行环境  Windows11 Visual Studio Code v2022 Node.js v16.5.01 Vue&#x2F;cli v5.0.6  需求案例说明： 使用 Vue2模块化编程方式来实现下图的案例（样式是参照教程随便写的，这里以熟悉Vue2为主）  项目结构：  说明： Vue2支持模块化编程，一个 Vue后缀的文件表示一个Vue">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/838cf4c9c8c64ca1871481759a9b92bb.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/9525158981a647ee8530c7e25d29dc98.png">
<meta property="article:published_time" content="2022-06-21T06:30:18.000Z">
<meta property="article:modified_time" content="2022-08-22T15:49:24.762Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Vue">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/838cf4c9c8c64ca1871481759a9b92bb.gif#pic_center">
  
  
  
  <title>Vue2 笔记1 基于Vue2的TodoList待办事项增删查案例 _ 基于Node.js 使用Vue-CLI脚手架构建项目 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Vue2 笔记1 基于Vue2的TodoList待办事项增删查案例 _ 基于Node.js 使用Vue-CLI脚手架构建项目"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-06-21 14:30" pubdate>
          2022年6月21日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          8.2k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          69 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Vue2 笔记1 基于Vue2的TodoList待办事项增删查案例 _ 基于Node.js 使用Vue-CLI脚手架构建项目</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[TOC]</p>
<h1 id="一、参考资料"><a href="#一、参考资料" class="headerlink" title="一、参考资料"></a>一、参考资料</h1><hr>
<ul>
<li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Zy4y1K7SH?p=70">视频资料</a></li>
</ul>
<h1 id="二、运行环境"><a href="#二、运行环境" class="headerlink" title="二、运行环境"></a>二、运行环境</h1><hr>
<ul>
<li>Windows11</li>
<li>Visual Studio Code v2022</li>
<li>Node.js v16.5.01</li>
<li>Vue&#x2F;cli v5.0.6</li>
</ul>
<p><strong>需求案例说明：</strong></p>
<p>使用 Vue2模块化编程方式来实现下图的案例（样式是参照教程随便写的，这里以熟悉Vue2为主）</p>
<p><img src="https://img-blog.csdnimg.cn/838cf4c9c8c64ca1871481759a9b92bb.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p><strong>项目结构：</strong></p>
<p><img src="https://img-blog.csdnimg.cn/9525158981a647ee8530c7e25d29dc98.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p><strong>说明：</strong></p>
<p>Vue2支持模块化编程，一个 Vue后缀的文件表示一个Vue组件，其中通常包括 ：</p>
<p>1） HTML 代码，即<code>&lt;template&gt;</code> 标签，<br>2） JS代码，即<code>&lt;script&gt;</code>标签 ，通常是返回 Key-Value 形式的对象，表示一些关于Vue组件的参数，包括变量和函数。<br>3）CSS样式代码，即<code>&lt;style&gt;</code> 标签，通常使用 scoped 限制样式只在当前组件内有效。</p>
<h1 id="三、根据需求进行分模块开发"><a href="#三、根据需求进行分模块开发" class="headerlink" title="三、根据需求进行分模块开发"></a>三、根据需求进行分模块开发</h1><p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/41df09db700a4a6faa8ffad5834bdeb1.png#pic_center">https://img-blog.csdnimg.cn/41df09db700a4a6faa8ffad5834bdeb1.png#pic_center</a> &#x3D;80%x)<br>根据界面可将这个Todo案例分为三个模块，分别是 Header、Body和Footer，其中Body中由多个待办事项组成，所以Body还可以分成多个TodoItem，所以共有4个模块。</p>
<p>需要注意的是，在Vue2工程中，App.vue类似于 SpringBoot里的启动类，是主要导入在HTML的组件，所以关于数据的存储和修改都将放在App.vue中，而其他的组件则需要通过App进行传递相关的数据或者函数来完成案例。</p>
<h1 id="四、代码实现"><a href="#四、代码实现" class="headerlink" title="四、代码实现"></a>四、代码实现</h1><h2 id="App-vue"><a href="#App-vue" class="headerlink" title="App.vue"></a>App.vue</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;app&quot;</span>&gt;</span><br>    <br>    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;./assets/logo.png&quot;</span>&gt;</span>基于Vue2的TodoList案例<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">TodoHeader</span> <span class="hljs-attr">:fnAddTodo</span>=<span class="hljs-string">&quot;fnAddTodo&quot;</span> &gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">TodoHeader</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">TodoBody</span> <span class="hljs-attr">:todoList</span>=<span class="hljs-string">&quot;todoList&quot;</span> <span class="hljs-attr">:fnCheckTodo</span>=<span class="hljs-string">&quot;fnCheckTodo&quot;</span> <span class="hljs-attr">:fnDeleteTodo</span>=<span class="hljs-string">&quot;fnDeleteTodo&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">TodoBody</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">TodoFooter</span> <span class="hljs-attr">:todoList</span>=<span class="hljs-string">&quot;todoList&quot;</span> <span class="hljs-attr">:fnUpdateTodo</span>=<span class="hljs-string">&quot;fnUpdateTodo&quot;</span> <span class="hljs-attr">:fnClearFinishedTodo</span>=<span class="hljs-string">&quot;fnClearFinishedTodo&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">TodoFooter</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">TodoHeader</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./components/TodoHeader.vue&#x27;</span></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">TodoBody</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./components/TodoBody.vue&#x27;</span></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">TodoFooter</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./components/TodoFooter.vue&#x27;</span></span><br><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;App&#x27;</span>,</span><br><span class="language-javascript">  <span class="hljs-title function_">data</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">    <span class="hljs-keyword">return</span> &#123;</span><br><span class="language-javascript">      <span class="hljs-attr">todoList</span>: [</span><br><span class="language-javascript">          &#123;<span class="hljs-attr">id</span>: <span class="hljs-string">&#x27;001&#x27;</span>, <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;吃饭&#x27;</span>, <span class="hljs-attr">done</span>: <span class="hljs-literal">true</span>&#125;,</span><br><span class="language-javascript">          &#123;<span class="hljs-attr">id</span>: <span class="hljs-string">&#x27;002&#x27;</span>, <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;睡觉&#x27;</span>, <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span>&#125;,</span><br><span class="language-javascript">          &#123;<span class="hljs-attr">id</span>: <span class="hljs-string">&#x27;003&#x27;</span>, <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;玩&#x27;</span>, <span class="hljs-attr">done</span>:<span class="hljs-literal">false</span>&#125;</span><br><span class="language-javascript">      ],</span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript">  &#125;,</span><br><span class="language-javascript">  <span class="hljs-attr">methods</span>: &#123;</span><br><span class="language-javascript">    <span class="hljs-comment">// 函数: 添加新的待办事项</span></span><br><span class="language-javascript">    <span class="hljs-title function_">fnAddTodo</span>(<span class="hljs-params">todoObj</span>)&#123;</span><br><span class="language-javascript">      <span class="hljs-variable language_">this</span>.<span class="hljs-property">todoList</span>.<span class="hljs-title function_">unshift</span>(todoObj)</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-comment">// 函数: 切换待办事项的状态</span></span><br><span class="language-javascript">    <span class="hljs-title function_">fnCheckTodo</span>(<span class="hljs-params">id</span>)&#123;</span><br><span class="language-javascript">      <span class="hljs-variable language_">this</span>.<span class="hljs-property">todoList</span>.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">todoObj</span>) =&gt;</span> &#123;</span><br><span class="language-javascript">        <span class="hljs-keyword">if</span>(id === todoObj.<span class="hljs-property">id</span>)</span><br><span class="language-javascript">          todoObj.<span class="hljs-property">done</span> = !todoObj.<span class="hljs-property">done</span></span><br><span class="language-javascript">      &#125;)</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-comment">// 函数: 删除待办事项</span></span><br><span class="language-javascript">    <span class="hljs-title function_">fnDeleteTodo</span>(<span class="hljs-params">id</span>)&#123;</span><br><span class="language-javascript">      <span class="hljs-variable language_">this</span>.<span class="hljs-property">todoList</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">todoList</span>.<span class="hljs-title function_">filter</span>(<span class="hljs-function"><span class="hljs-params">todoObj</span> =&gt;</span> todoObj.<span class="hljs-property">id</span> !== id)</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-comment">// 函数: 批量操作待办事项的状态</span></span><br><span class="language-javascript">    <span class="hljs-title function_">fnUpdateTodo</span>(<span class="hljs-params">done</span>)&#123;</span><br><span class="language-javascript">      <span class="hljs-variable language_">this</span>.<span class="hljs-property">todoList</span>.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">todoObj</span>) =&gt;</span> todoObj.<span class="hljs-property">done</span> = done)</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-comment">// 函数: 清除已完成的待办事项</span></span><br><span class="language-javascript">    <span class="hljs-title function_">fnClearFinishedTodo</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">      <span class="hljs-keyword">if</span>(<span class="hljs-title function_">confirm</span>(<span class="hljs-string">&#x27;确认要删除吗?&#x27;</span>))</span><br><span class="language-javascript">        <span class="hljs-variable language_">this</span>.<span class="hljs-property">todoList</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">todoList</span>.<span class="hljs-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">todoObj</span>) =&gt;</span> !todoObj.<span class="hljs-property">done</span>)</span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript">  &#125;,</span><br><span class="language-javascript">  <span class="hljs-attr">components</span>: &#123;<span class="hljs-title class_">TodoHeader</span>, <span class="hljs-title class_">TodoBody</span>, <span class="hljs-title class_">TodoFooter</span>&#125;</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">scoped</span>&gt;</span><span class="language-css"></span><br><span class="language-css">  <span class="hljs-selector-tag">h1</span>&#123; <span class="hljs-attribute">text-align</span>: center;&#125;</span><br><span class="language-css">  <span class="hljs-selector-tag">img</span>&#123; <span class="hljs-attribute">width</span>: <span class="hljs-number">50px</span>; <span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;&#125;</span><br><span class="language-css">  <span class="hljs-selector-id">#app</span>&#123;</span><br><span class="language-css">    <span class="hljs-attribute">width</span>: <span class="hljs-number">600px</span>;</span><br><span class="language-css">    <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> auto;</span><br><span class="language-css">  &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><br></code></pre></td></tr></table></figure>

<h2 id="TodoHeader-vue"><a href="#TodoHeader-vue" class="headerlink" title="TodoHeader.vue"></a>TodoHeader.vue</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">&quot;add&quot;</span>&gt;</span>待办事项<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;add&quot;</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;title&quot;</span> @<span class="hljs-attr">keydown.enter</span>=<span class="hljs-string">&quot;add&quot;</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">&quot;请输入任务标题, 敲回车确认&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;add&quot;</span>&gt;</span>添加<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-comment">// 第三方库 npm i nanoid</span></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> &#123;nanoid&#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;nanoid&#x27;</span> </span><br><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;TodoHeader&#x27;</span>,</span><br><span class="language-javascript">    <span class="hljs-title function_">data</span>(<span class="hljs-params"></span>) &#123;</span><br><span class="language-javascript">        <span class="hljs-keyword">return</span> &#123; </span><br><span class="language-javascript">            <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;&#x27;</span></span><br><span class="language-javascript">        &#125;</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-attr">methods</span>: &#123;</span><br><span class="language-javascript">        <span class="hljs-comment">// 添加Todo</span></span><br><span class="language-javascript">        <span class="hljs-title function_">add</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">            <span class="hljs-keyword">if</span>(!<span class="hljs-variable language_">this</span>.<span class="hljs-property">title</span>.<span class="hljs-title function_">trim</span>()) <span class="hljs-keyword">return</span> <span class="hljs-title function_">alert</span>(<span class="hljs-string">&#x27;提示:输入的待办事项不能为空!&#x27;</span>)</span><br><span class="language-javascript">            <span class="hljs-comment">// 封装 Todo 对象</span></span><br><span class="language-javascript">            <span class="hljs-keyword">const</span> todoObj = &#123;</span><br><span class="language-javascript">                <span class="hljs-attr">id</span>: <span class="hljs-title function_">nanoid</span>(),</span><br><span class="language-javascript">                <span class="hljs-attr">title</span>: <span class="hljs-variable language_">this</span>.<span class="hljs-property">title</span>,</span><br><span class="language-javascript">                <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span></span><br><span class="language-javascript">            &#125;</span><br><span class="language-javascript">            <span class="hljs-comment">// 调用回调函数</span></span><br><span class="language-javascript">            <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">fnAddTodo</span>(todoObj)</span><br><span class="language-javascript">            <span class="hljs-comment">// 清空</span></span><br><span class="language-javascript">            <span class="hljs-variable language_">this</span>.<span class="hljs-property">title</span> = <span class="hljs-string">&#x27;&#x27;</span></span><br><span class="language-javascript">        &#125;</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-comment">// 引入上层App的方法</span></span><br><span class="language-javascript">    <span class="hljs-attr">props</span>: [<span class="hljs-string">&#x27;fnAddTodo&#x27;</span>]</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">scoped</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-selector-tag">div</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">background</span>: gray;</span><br><span class="language-css">        <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;</span><br><span class="language-css">        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.2em</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">button</span>, <span class="hljs-selector-tag">input</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.1em</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">button</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">float</span>: right;</span><br><span class="language-css"></span><br><span class="language-css">    &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="TodoBody-vue"><a href="#TodoBody-vue" class="headerlink" title="TodoBody.vue"></a>TodoBody.vue</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;todoObj in todoList&quot;</span> <span class="hljs-attr">:key</span>=<span class="hljs-string">&quot;todoObj.id&quot;</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">TodoItem</span> <span class="hljs-attr">:todoObj</span>=<span class="hljs-string">&quot;todoObj&quot;</span> <span class="hljs-attr">:fnCheckTodo</span>=<span class="hljs-string">&quot;fnCheckTodo&quot;</span> <span class="hljs-attr">:fnDeleteTodo</span>=<span class="hljs-string">&quot;fnDeleteTodo&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">TodoItem</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">TodoItem</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./TodoItem.vue&#x27;</span></span><br><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;TodoBody&#x27;</span>,</span><br><span class="language-javascript">    <span class="hljs-attr">components</span>: &#123;<span class="hljs-title class_">TodoItem</span>&#125;,</span><br><span class="language-javascript">    <span class="hljs-attr">props</span>: [<span class="hljs-string">&#x27;todoList&#x27;</span>, <span class="hljs-string">&#x27;fnCheckTodo&#x27;</span>, <span class="hljs-string">&#x27;fnDeleteTodo&#x27;</span>]</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">scoped</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-selector-tag">div</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">background</span>: pink;</span><br><span class="language-css">        <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;</span><br><span class="language-css">        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.1em</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">ul</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">list-style</span>: none;</span><br><span class="language-css">        <span class="hljs-attribute">padding-left</span>: <span class="hljs-number">0px</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br></code></pre></td></tr></table></figure>
<h2 id="TodoItem-vue"><a href="#TodoItem-vue" class="headerlink" title="TodoItem.vue"></a>TodoItem.vue</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;checkbox&quot;</span> <span class="hljs-attr">:checked</span>=<span class="hljs-string">&quot;todoObj.done&quot;</span> @<span class="hljs-attr">change</span>=<span class="hljs-string">&quot;handlerCheckTodo(todoObj.id)&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>&#123;&#123; todoObj.title &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;handlerDeleteTodo(todoObj.id)&quot;</span>&gt;</span>删除<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;TodoItem&#x27;</span>,</span><br><span class="language-javascript">    <span class="hljs-attr">methods</span>: &#123;</span><br><span class="language-javascript">        <span class="hljs-title function_">handlerCheckTodo</span>(<span class="hljs-params">id</span>)&#123;</span><br><span class="language-javascript">            <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">fnCheckTodo</span>(id)</span><br><span class="language-javascript">        &#125;,</span><br><span class="language-javascript">        <span class="hljs-title function_">handlerDeleteTodo</span>(<span class="hljs-params">id</span>)&#123;</span><br><span class="language-javascript">            <span class="hljs-keyword">if</span>(<span class="hljs-title function_">confirm</span>(<span class="hljs-string">&#x27;确认要删除Id为 [&#x27;</span> + id + <span class="hljs-string">&#x27;] 的待办事项吗?&#x27;</span>))</span><br><span class="language-javascript">                <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">fnDeleteTodo</span>(id)</span><br><span class="language-javascript">        &#125;</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-comment">// 引入Body传来的todoObj 和 App传来的 fnCheckTodo方法</span></span><br><span class="language-javascript">    <span class="hljs-attr">props</span>: [<span class="hljs-string">&#x27;todoObj&#x27;</span>, <span class="hljs-string">&#x27;fnCheckTodo&#x27;</span>, <span class="hljs-string">&#x27;fnDeleteTodo&#x27;</span>],</span><br><span class="language-javascript"></span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">scoped</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-selector-tag">nav</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">background</span>: <span class="hljs-built_in">rgb</span>(<span class="hljs-number">196</span>, <span class="hljs-number">100</span>, <span class="hljs-number">100</span>);</span><br><span class="language-css">        <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;</span><br><span class="language-css">        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.1em</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">button</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">float</span><span class="hljs-selector-pseudo">:right</span>;</span><br><span class="language-css">        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.1em</span>;</span><br><span class="language-css">        <span class="hljs-attribute">display</span>: none;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">input</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">width</span>:<span class="hljs-number">1.5em</span>;</span><br><span class="language-css">        <span class="hljs-attribute">height</span>:<span class="hljs-number">1.5em</span></span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">nav</span><span class="hljs-selector-pseudo">:hover</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">background</span>: <span class="hljs-built_in">rgb</span>(<span class="hljs-number">31</span>, <span class="hljs-number">101</span>, <span class="hljs-number">167</span>);</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">nav</span><span class="hljs-selector-pseudo">:hover</span> <span class="hljs-selector-tag">button</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">display</span>: block;</span><br><span class="language-css">    &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><br></code></pre></td></tr></table></figure>

<h2 id=""><a href="#" class="headerlink" title=""></a></h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;checkbox&quot;</span> <span class="hljs-attr">:checked</span>=<span class="hljs-string">&quot;todoObj.done&quot;</span> @<span class="hljs-attr">change</span>=<span class="hljs-string">&quot;handlerCheckTodo(todoObj.id)&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>&#123;&#123; todoObj.title &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;handlerDeleteTodo(todoObj.id)&quot;</span>&gt;</span>删除<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;TodoItem&#x27;</span>,</span><br><span class="language-javascript">    <span class="hljs-attr">methods</span>: &#123;</span><br><span class="language-javascript">        <span class="hljs-title function_">handlerCheckTodo</span>(<span class="hljs-params">id</span>)&#123;</span><br><span class="language-javascript">            <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">fnCheckTodo</span>(id)</span><br><span class="language-javascript">        &#125;,</span><br><span class="language-javascript">        <span class="hljs-title function_">handlerDeleteTodo</span>(<span class="hljs-params">id</span>)&#123;</span><br><span class="language-javascript">            <span class="hljs-keyword">if</span>(<span class="hljs-title function_">confirm</span>(<span class="hljs-string">&#x27;确认要删除Id为 [&#x27;</span> + id + <span class="hljs-string">&#x27;] 的待办事项吗?&#x27;</span>))</span><br><span class="language-javascript">                <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">fnDeleteTodo</span>(id)</span><br><span class="language-javascript">        &#125;</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-comment">// 引入Body传来的todoObj 和 App传来的 fnCheckTodo方法</span></span><br><span class="language-javascript">    <span class="hljs-attr">props</span>: [<span class="hljs-string">&#x27;todoObj&#x27;</span>, <span class="hljs-string">&#x27;fnCheckTodo&#x27;</span>, <span class="hljs-string">&#x27;fnDeleteTodo&#x27;</span>],</span><br><span class="language-javascript"></span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">scoped</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-selector-tag">nav</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">background</span>: <span class="hljs-built_in">rgb</span>(<span class="hljs-number">196</span>, <span class="hljs-number">100</span>, <span class="hljs-number">100</span>);</span><br><span class="language-css">        <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;</span><br><span class="language-css">        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.1em</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">button</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">float</span><span class="hljs-selector-pseudo">:right</span>;</span><br><span class="language-css">        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.1em</span>;</span><br><span class="language-css">        <span class="hljs-attribute">display</span>: none;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">input</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">width</span>:<span class="hljs-number">1.5em</span>;</span><br><span class="language-css">        <span class="hljs-attribute">height</span>:<span class="hljs-number">1.5em</span></span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">nav</span><span class="hljs-selector-pseudo">:hover</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">background</span>: <span class="hljs-built_in">rgb</span>(<span class="hljs-number">31</span>, <span class="hljs-number">101</span>, <span class="hljs-number">167</span>);</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">nav</span><span class="hljs-selector-pseudo">:hover</span> <span class="hljs-selector-tag">button</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">display</span>: block;</span><br><span class="language-css">    &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br><br></code></pre></td></tr></table></figure>
<h2 id="TodoFooter-vue"><a href="#TodoFooter-vue" class="headerlink" title="TodoFooter.vue"></a>TodoFooter.vue</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">nav</span> <span class="hljs-attr">v-show</span>=<span class="hljs-string">&quot;totalTodoList&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;checkbox&quot;</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;isAll&quot;</span>&gt;</span><br>        已完成 &#123;&#123; totalFinishedTodo &#125;&#125; / 全部 &#123;&#123; totalTodoList &#125;&#125;<br>        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;fnClearFinishedTodo&quot;</span>&gt;</span>清除已完成<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;TodoFooter&#x27;</span>,</span><br><span class="language-javascript">    <span class="hljs-attr">props</span>: [<span class="hljs-string">&#x27;todoList&#x27;</span>, <span class="hljs-string">&#x27;fnUpdateTodo&#x27;</span>, <span class="hljs-string">&#x27;fnClearFinishedTodo&#x27;</span>],</span><br><span class="language-javascript">    <span class="hljs-comment">// 计算属性</span></span><br><span class="language-javascript">    <span class="hljs-attr">computed</span>:&#123;</span><br><span class="language-javascript">        <span class="hljs-comment">// 计算已完成的待办事项</span></span><br><span class="language-javascript">        <span class="hljs-title function_">totalFinishedTodo</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">            <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">todoList</span>.<span class="hljs-title function_">reduce</span>(<span class="hljs-function">(<span class="hljs-params">pre, todoObj</span>) =&gt;</span> pre + (todoObj.<span class="hljs-property">done</span> ? <span class="hljs-number">1</span> : <span class="hljs-number">0</span>), <span class="hljs-number">0</span>) </span><br><span class="language-javascript">        &#125;,</span><br><span class="language-javascript">        <span class="hljs-comment">// 所有的待办事项</span></span><br><span class="language-javascript">        <span class="hljs-title function_">totalTodoList</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">            <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">todoList</span>.<span class="hljs-property">length</span></span><br><span class="language-javascript">        &#125;,</span><br><span class="language-javascript">        <span class="hljs-attr">isAll</span>: &#123;</span><br><span class="language-javascript">            <span class="hljs-comment">// 全选按钮默认的勾选条件: 所有待办事项都已经完成</span></span><br><span class="language-javascript">            <span class="hljs-title function_">get</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">                <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">totalTodoList</span> &gt; <span class="hljs-number">0</span> &amp;&amp; <span class="hljs-variable language_">this</span>.<span class="hljs-property">totalFinishedTodo</span> == <span class="hljs-variable language_">this</span>.<span class="hljs-property">totalTodoList</span></span><br><span class="language-javascript">            &#125;,</span><br><span class="language-javascript">            <span class="hljs-title function_">set</span>(<span class="hljs-params">value</span>)&#123;</span><br><span class="language-javascript">                <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">fnUpdateTodo</span>(value)</span><br><span class="language-javascript">            &#125;</span><br><span class="language-javascript">        &#125;</span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">scoped</span>&gt;</span><span class="language-css"></span><br><span class="language-css">    <span class="hljs-selector-tag">nav</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">background</span>: greenyellow;</span><br><span class="language-css">        <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;</span><br><span class="language-css">        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.1em</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">input</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">height</span>: <span class="hljs-number">1.5em</span>;</span><br><span class="language-css">        <span class="hljs-attribute">width</span>: <span class="hljs-number">1.5em</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css">    <span class="hljs-selector-tag">button</span>&#123;</span><br><span class="language-css">        <span class="hljs-attribute">float</span><span class="hljs-selector-pseudo">:right</span>;</span><br><span class="language-css">        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1.1em</span>;</span><br><span class="language-css">    &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h1 id="五、要点总结"><a href="#五、要点总结" class="headerlink" title="五、要点总结"></a>五、要点总结</h1><h2 id="5-1-Vue2组件化编码流程"><a href="#5-1-Vue2组件化编码流程" class="headerlink" title="5.1 Vue2组件化编码流程"></a>5.1 Vue2组件化编码流程</h2><ol>
<li>拆分静态组件：组件要按照功能要点拆分，命名不要和Html元素冲突 </li>
<li>实现动态组件：考虑好数据的存放位置，数据是单个组件在用还是多个组件在用。</li>
</ol>
<ul>
<li>单个组件在用：放在组件本身即可</li>
<li>多个组件在用：放在他们之间的共同上层组件上（高内聚，低耦合）</li>
</ul>
<ol start="3">
<li>实现交互：从绑定事件开始</li>
</ol>
<h2 id="5-2-props适用场景"><a href="#5-2-props适用场景" class="headerlink" title="5.2 props适用场景"></a>5.2 props适用场景</h2><ol>
<li>父组件 &#x3D;&#x3D;&gt; 子组件 进行数据通信</li>
<li>子组件 &#x3D;&#x3D;&gt; 父组件 通信（要求父组件先给子组件一个函数）</li>
</ol>
<h2 id="5-3-慎用v-model"><a href="#5-3-慎用v-model" class="headerlink" title="5.3 慎用v-model"></a>5.3 慎用v-model</h2><p>v-model 属性可实现DOM数据与Vue对象属性的双向绑定，但是v-model绑定的值不能是 <code>props</code> 传递过来的值，因为<code>props</code>是不可以修改的。</p>
<h2 id="5-4-props"><a href="#5-4-props" class="headerlink" title="5.4 props"></a>5.4 props</h2><p><code>props</code> 传递到组件的值若是对象类型的值，那么修改对象中的属性时，Vue并不会报错，但不推荐这样使用。</p>
<h2 id="5-5-个人总结"><a href="#5-5-个人总结" class="headerlink" title="5.5 个人总结"></a>5.5 个人总结</h2><p>这几天刚正式接触 Vue，毕竟是封装的框架，感觉比较容易上手，而且工程化的设计思想和后端设计思路类似，早知道之前就用这个框架做课设了，之前是用的JQuery，导致HTML、CSS和JavaScript三者之间关系比较混乱，现在用Vue的组件化编程，代码优雅许多。</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E5%89%8D%E7%AB%AF/" class="category-chain-item">前端</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Vue/">#Vue</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Vue2 笔记1 基于Vue2的TodoList待办事项增删查案例 _ 基于Node.js 使用Vue-CLI脚手架构建项目</div>
      <div>http://example.com/2022/06/21/Vue2 笔记1 基于Vue2的TodoList待办事项增删查案例 _ 基于Node.js 使用Vue-CLI脚手架构建项目/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年6月21日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/06/21/Vue2%20%E7%AC%94%E8%AE%B02%20%E5%9F%BA%E4%BA%8EVue2%E7%9A%84TodoList%E5%BE%85%E5%8A%9E%E4%BA%8B%E9%A1%B9%E6%A1%88%E4%BE%8B%20_%20localStorage%E6%9C%AC%E5%9C%B0%E5%AD%98%E5%82%A8%20_%20Vue2%E7%9A%84%E5%8F%91%E5%B8%83%E4%B8%8E%E8%AE%A2%E9%98%85%20_%20Vue2%E6%94%AF%E6%8C%81%E7%9A%84%E5%8A%A8%E7%94%BB%E7%B1%BB%20_JavaScript%E5%8E%9F%E5%9E%8B%E5%AF%B9%E8%B1%A1/" title="Vue2 笔记2 基于Vue2的TodoList待办事项案例 _ localStorage本地存储 _ Vue2的发布与订阅 _ Vue2支持的动画类 _JavaScript原型对象">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Vue2 笔记2 基于Vue2的TodoList待办事项案例 _ localStorage本地存储 _ Vue2的发布与订阅 _ Vue2支持的动画类 _JavaScript原型对象</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/05/20/%E8%AF%BE%E8%AE%BE%E6%80%BB%E7%BB%93%20_%20%E5%88%9D%E5%AD%A6%E8%80%85SpringBoot%E6%A1%86%E6%9E%B6%E5%BC%80%E5%8F%91%E5%AD%98%E5%9C%A8%E7%9A%84%E9%97%AE%E9%A2%98%E6%80%BB%E7%BB%93%20_%20%E5%95%86%E5%9F%8E%E7%B3%BB%E7%BB%9F%E7%9A%84%E8%AE%BE%E8%AE%A1%E9%97%AE%E9%A2%98%E6%80%BB%E7%BB%93/" title="课设总结 _ 初学者SpringBoot框架开发存在的问题总结 _ 商城系统的设计问题总结">
                        <span class="hidden-mobile">课设总结 _ 初学者SpringBoot框架开发存在的问题总结 _ 商城系统的设计问题总结</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
